import React, { PureComponent, Fragment } from 'react';
import {
  Form,
  Input,
  Select,
  Modal,
  Radio,
} from 'antd';


const FormItem = Form.Item;
const { Option } = Select;

const RadioGroup = Radio.Group;

//更新和新增用户信息   modalVisible

@Form.create()
class UpdateForm extends PureComponent {
  constructor(props) {
    super(props);


    this.formLayout = {
      labelCol: { span: 7 },
      wrapperCol: { span: 13 },
    };
  }

  // 表单
  renderContent = () => {
    const { form, values, data: { data }, userList } = this.props;
    const children = [];
    const userArr = [];
    let adminNo = '';
    if (data) {
      for (let i = 0; i < data.length; i++) {
        children.push(<Option key={data[i].deptNo}>{data[i].deptName}</Option>);
      }
    }
    if (userList) {
      for (let i = 0; i < userList.length; i++) {
        userArr.push(<Option key={userList[i].adminNo}>{userList[i].realName}</Option>);
        if (values.adminName === userList[i].realName) {
          adminNo = userList[i].adminNo;
        }
      }
    }
    console.log(values);
    return [
      <FormItem key="deptName" {...this.formLayout} label="部门名称">
        {form.getFieldDecorator('deptName', {
          rules: [{ required: true, message: '请输入部门名称！' }],
          initialValue: values.deptName,
        })(<Input placeholder="请输入"/>)}
      </FormItem>,

      <FormItem key="adminNo" {...this.formLayout} label="负责人">
        {form.getFieldDecorator('adminNo', {
          rules: [{ required: false, message: '请输入负责人！' }],
          initialValue: adminNo,
        })(
          <Select
            style={{ width: '100%' }}
            placeholder="请输入部门">
            {userArr}
          </Select>,
        )}
      </FormItem>,
      <FormItem key="parentNo" {...this.formLayout} label="上级部门">
        {form.getFieldDecorator('parentNo', {
          rules: [{ required: false, message: '请输选择上级部门！' }],
          initialValue: values.parentNo,
        })(
          <Select
            style={{ width: '100%' }}
            placeholder="请输选择上级部门">
            {children}
          </Select>,
        )}
      </FormItem>,

      <FormItem key="status" {...this.formLayout} label="状态">
        {form.getFieldDecorator('status', {
          rules: [{ required: true, message: '请选择状态！' }],
          initialValue: values.status,
        })(
          <RadioGroup>
            <Radio value={1}>启用</Radio>
            <Radio value={2}>停用</Radio>
          </RadioGroup>,
        )}
      </FormItem>,

    ];
  };

  //弹窗
  render() {
    const { updateModalVisible, handleUpdateModalVisible, modalVisible } = this.props;

    //完成
    const handleNext = () => {
      const { form, values, handleUpdate, handleAdd, modalVisible } = this.props;
      form.validateFields((err, fieldsValue) => {
        if (err) return;
        const formVals = { ...values, ...fieldsValue };
        if (modalVisible) {
          handleAdd(formVals);
        } else {
          handleUpdate(formVals);
        }
      });
    };
    return (
      <Modal
        width={640}
        bodyStyle={{ padding: '32px 40px 48px' }}
        destroyOnClose
        title={modalVisible ? '新增部门' : '修改部门'}
        visible={updateModalVisible}
        onOk={handleNext}
        onCancel={() => handleUpdateModalVisible(false, [])}
      >
        {this.renderContent()}
      </Modal>
    );
  }
}

export default UpdateForm;
